﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmDangKy : Form
    {
        private static frmDangKy frm = null;

        private DangKy dangky = null;
        private List<string> hocky = new List<string>();
        private List<string> hockyMH = new List<string>();
        private List<string> namhoc = new List<string>();
        private List<string> namhocMH = new List<string>();

        private frmDangKy()
        {
            InitializeComponent();
            hockyMH.Add("Chọn");
            namhocMH.Add("Chọn");
            for (int i = 0; i < 8; i++)
            {
                if (i < 3)
                {
                    hocky.Add((i + 1).ToString());
                    hockyMH.Add((i + 1).ToString());
                }
                namhoc.Add((i + 1).ToString());
                namhocMH.Add((i + 1).ToString());
            }
        }

        public static frmDangKy Create()
        {
            return Create(false);
        }

        public static frmDangKy Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmDangKy();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            return frm;
        }

        private void frmDangKy_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm.Dispose();
            frm = null;
        }

        private void frmDangKy_Load(object sender, EventArgs e)
        {
            dangky = new DangKy();
            cmbHocKy.DataSource = hocky;
            cmbNamHoc.DataSource = namhoc;
            cmbHocKyMH.DataSource = hockyMH;
            cmbNamHocMH.DataSource = namhocMH;
        }

        private void ClearDataDataGridView()
        {
            while (dgvDangDy.Rows.Count > 1)
            {
                dgvDangDy.Rows.RemoveAt(0);
            }
            lblTS.Text = "0";
        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            if (dgvDangDy.Rows.Count - 1 > 0)
            {
                DangKy dk = new DangKy();
                for (int i = 0; i < dgvDangDy.Rows.Count - 1; i++)
                {
                    DangKyDTO dsdk = new DangKyDTO();
                    dsdk.MaSV = txtMaSV.Text;
                    dsdk.MaMH = int.Parse(dgvDangDy.Rows[i].Cells["cMaDK"].Value.ToString());
                    dsdk.MaLop = int.Parse(dgvDangDy.Rows[i].Cells["cMaLop"].Value.ToString());
                    dsdk.HocKy = byte.Parse(cmbHocKy.Text);
                    dsdk.NamHoc = byte.Parse(cmbNamHoc.Text);
                    dk.DanhSach.Add(dsdk);
                }
                int count = dk.Add();
                if (count > 0)
                {
                    MessageBox.Show("Đã đăng ký được " + count.ToString() + " môn học", "Thông báo");
                    ClearDataDataGridView();
                    btnThongTin.PerformClick();
                }
                else
                {
                    MessageBox.Show("Có lỗi trong quá trình đăng ký", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập danh sách môn đăng ký", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnThongTin_Click(object sender, EventArgs e)
        {
            txtMaSV.Text = txtMaSV.Text.Trim();
            if (!string.IsNullOrEmpty(txtMaSV.Text))
            {
                SinhVien sv = new SinhVien();
                #region Sinh viên
                if (!sv.Info(txtMaSV.Text))
                {
                    MessageBox.Show("Không có sinh viên có mã sinh viên đó", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaSV.Focus();
                    return;
                }
                lblTenSV.Text = sv.ThongTin.Ten;
                lblLopSV.Text = sv.ThongTin.Lop;
                int ngay = sv.ThongTin.NgaySinh.Day;
                int thang = sv.ThongTin.NgaySinh.Month;
                string ngaysinh = "";
                if (ngay < 10)
                {
                    ngaysinh += "0" + ngay;
                }
                else
                {
                    ngaysinh += ngay;
                }
                if (thang < 10)
                {
                    ngaysinh += "-0" + thang;
                }
                else
                {
                    ngaysinh += thang;
                }
                ngaysinh += "-" + sv.ThongTin.NgaySinh.Year;
                lblNgaySinhSV.Text = ngaysinh;
                lblMaSV.Text = txtMaSV.Text;
                #endregion
                cmbNamHocMH.SelectedIndex = 0;
                cmbHocKyMH.SelectedIndex = 0;
                dgvMonHoc.DataSource = dangky.List(txtMaSV.Text);
                int[] NamHocvaHocKy = dangky.NamHocvaHocKy(txtMaSV.Text);
                string namhocdk = "1";
                string hockydk = "1";
                if (NamHocvaHocKy[0] != 0)
                {
                    if (NamHocvaHocKy[1] == 1)
                    {
                        namhocdk = NamHocvaHocKy[0].ToString();
                        hockydk = "2";
                    }
                    else
                    {
                        if (NamHocvaHocKy[0] < int.Parse(namhoc[namhoc.Count - 1]))
                        {
                            hockydk = "1";
                            namhocdk = (NamHocvaHocKy[0] + 1).ToString();
                        }
                        else
                        {
                            namhocdk = NamHocvaHocKy[0].ToString();
                            hockydk = NamHocvaHocKy[1].ToString();
                        }
                    }
                }
                cmbNamHoc.Text = namhocdk;
                cmbHocKy.Text = hockydk;
            }
            else
            {
                MessageBox.Show("Phải nhập mã sinh viên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtMaSV.Focus();
                return;
            }
        }

        private void dgvMonHocDangDy_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MonHoc mh = new MonHoc();
            if (dgvDangDy.Rows[e.RowIndex].Cells["cMaDK"].Value != null)
            {
                for (int i = 0; i < dgvDangDy.Rows.Count - 1; i++)
                {
                    if (e.RowIndex == i)
                    {
                        continue;
                    }
                    if (dgvDangDy.Rows[i].Cells["cMaDK"].Value != null && dgvDangDy.Rows[e.RowIndex].Cells["cMaDK"].Value.ToString().Trim() == dgvDangDy.Rows[i].Cells["cMaDK"].Value.ToString().Trim())
                    {
                        MessageBox.Show("Mã môn học đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        dgvDangDy.Rows.RemoveAt(e.RowIndex);
                        return;
                    }
                }
                mh.ThongTin.Ma = dgvDangDy.Rows[e.RowIndex].Cells["cMaDK"].Value.ToString();
            }
            else
            {
                mh.ThongTin.Ma = "";
            }
            string[] temp = mh.FindName();
            dgvDangDy.Rows[e.RowIndex].Cells["cTenDK"].Value = temp[1];
            dgvDangDy.Rows[e.RowIndex].Cells["cSoTCDK"].Value = temp[2];
            Lop l = new Lop();
            string malop = "";
            if (dgvDangDy.Rows[e.RowIndex].Cells["cMaLop"].Value != null)
            {
                malop = dgvDangDy.Rows[e.RowIndex].Cells["cMaLop"].Value.ToString();
            }
            dgvDangDy.Rows[e.RowIndex].Cells["cTenLop"].Value = l.FindName(malop);
            lblTS.Text = (dgvDangDy.Rows.Count - 1).ToString();
        }

        private void txtMaSV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnThongTin.PerformClick();
            }
        }

        private void DanhSachDangKy(string MaSinhVien, string NamHoc, string HocKy)
        {
            if (NamHoc == "Chọn")
            {
                NamHoc = "";
            }
            if (HocKy == "Chọn")
            {
                HocKy = "";
            }
            dgvMonHoc.DataSource = dangky.Search(MaSinhVien, NamHoc, HocKy);
        }

        private void cmbNamHocMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNamHocMH.SelectedIndex == 0)
            {
                cmbHocKyMH.SelectedIndex = 0;
            }
            DanhSachDangKy(lblMaSV.Text, cmbNamHocMH.Text, cmbHocKyMH.Text);
        }

        private void cmbHocKyMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbHocKyMH.SelectedIndex != 0)
            {
                if (cmbNamHocMH.SelectedIndex == 0)
                {
                    cmbNamHocMH.SelectedIndex = 1;
                }
            }
            DanhSachDangKy(lblMaSV.Text, cmbNamHocMH.Text, cmbHocKyMH.Text);
        }

        private void txtMaSV_Leave(object sender, EventArgs e)
        {
            txtMaSV.Text = txtMaSV.Text.Trim();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvMonHoc.SelectedRows.Count > 0)
            {
                DialogResult dalrs = MessageBox.Show("Bạn có chắc chán muốn xóa " + dgvMonHoc.SelectedRows.Count.ToString() + " đăng ký không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dalrs == DialogResult.OK)
                {
                    int count = 0;
                    int length = dgvMonHoc.SelectedRows.Count;
                    DangKy ds = new DangKy();
                    foreach (DataGridViewRow item in dgvMonHoc.SelectedRows)
                    {
                        DangKyDTO dsdk = new DangKyDTO();
                        dsdk.Ma = int.Parse(item.Cells["cMa"].Value.ToString());
                        ds.DanhSach.Add(dsdk);
                        int check = ds.Remove();
                        if (check > 0)
                        {
                            count++;
                            dgvMonHoc.Rows.Remove(item);
                        }
                    }
                    if (count > 0)
                    {
                        MessageBox.Show("Đã xóa thành công " + count.ToString() + " đăng ký", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn các đăng ký muốn xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mniXoa_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }
    }
}
